﻿using And.Shp.Common.ServiceLocation;
using Microsoft.SharePoint;
using SharepointCommon;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Uportal.Shp.Core.Common;
using Uportal.Shp.Core.Const;
using Uportal.Shp.Core.Enums;
using Uportal.Shp.Core.Services;

namespace Uportal.Shp.UI.Common
{
    public class FeedbackManager :IDisposable
    {
        private readonly IQueryWeb _queryWeb;
        private readonly ILogger _logger;
        //private readonly IAppPackage _app;
        //private SPListItemCollection 
        

        public FeedbackManager()
        {
            var sl = SL.GetCurrent();
            var url = Settings.Get().WebUrl;

            _queryWeb = WebFactory.Elevated(url);
            _logger = sl.GetInstance<ILogger>();
        }

        public void Dispose()
        {
            _queryWeb.Dispose();
        }

        /// <summary>
        /// Получение всех элементов, котрые не отвечены с прошлой недели
        /// </summary>
        /// <param name="dateTime"></param>
        /// <returns></returns>
        internal SPListItemCollection GetReport()
        {
            var list = _queryWeb.Web.Lists[ListInfo.FeedbackToManagmentListName];
            //string q = Q.Where(Q.Eq(Q.FieldRef(FieldInfo.Feedback.StateQuestion),Q.Value("Text",EnumResolver.GetStringValue(StateQuestion.Approved))));
            
            
            string q = Q.Where(
                                Q.And(
                                    Q.Eq(
                                        Q.FieldRef(FieldInfo.Feedback.StateQuestion),
                                        Q.Value("Text",EnumResolver.GetStringValue(StateQuestion.Approved)
                                        )),
                                        Q.IsNull( Q.FieldRef(FieldInfo.Feedback.DateOfAnswer)
                                        )
                                    )
                                );

            _logger.Info(q);
            
            SPListItemCollection items = list.GetItems(new SPQuery() { Query = q });
            
            if(items.Count == 0)
                return null;
            foreach (SPListItem item in items)
            {
                _logger.Info(item.ParentList.Forms[PAGETYPE.PAGE_DISPLAYFORM].ServerRelativeUrl);
                _logger.Info(_queryWeb.Web.Url);
            }
            return items;
        }

        internal string GetFormFullUrl(SPListItem item, PAGETYPE type)
        {
            return _queryWeb.Web.Url + item.ParentList.Forms[type].ServerRelativeUrl + "?ID=" + item.ID.ToString();
        }

        public static string SubjForModerator { get{ return "Утверждение вопроса"; } }

        public static string SubjForNamager { get { return "Необходим ответ на вопрос"; } }
    }
}